﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using CoolCode.Entities;
using CoolCode.Services;
using CoolCode.Web;
using CoolCode.Linq;
using CoolCode.WebServices;

namespace CoolCode.Web.UI.Views.Inquiry
{
    public partial class NGProducts : SearchPage<Inspection, IInspectionService>
    {
        private ICriterionService _CriterionService;

        public ICriterionService CriterionService
        {
            get
            {
                if (_CriterionService == null)
                {
                    _CriterionService = new CriterionService(Config.AppData, new WebCacheStrategy());
                }
                return _CriterionService;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            handleEvents();
            if (!IsPostBack)
            {
            }
        }

        private IQueryBuilder<Inspection> _queryBuilder;

        public override IQueryBuilder<Inspection> CreateFilter()
        {
            if (_queryBuilder == null)
            {
                List<ProductCriterion> criterions = this.CriterionService.List();
                _queryBuilder = productFilter.CreateFilter()
                     .Custom(QueryHelper.NGCondition(criterions));
            }
            return _queryBuilder;

            //.Custom(QueryHelper.PassCondition(criterions));
            //.Between(c => c.ODFar, criterion.MinODFar, criterion.MaxODFar)
            //.Between(c => c.ODClose, criterion.MinODClose, criterion.MaxODClose)
            //.Between(c => c.ConeHeight, criterion.MinConeHeight, criterion.MaxConeHeight)
            //.Between(c => c.SacculusLength, criterion.MinSacculusLength, criterion.MaxSacculusLength)
            //.Between(c => c.ODStressTest2, criterion.MinODStressTest2, criterion.MaxODStressTest2)
            //.Between(c => c.StressTestLength, criterion.MinStressTestLength, criterion.MaxStressTestLength);
        }

        protected void handleEvents()
        {
            this.Setup(lnkSearch, productList, c =>
            {
                productList.Model = c.Result;
                productList.DataBind();
                udpHeader.Update();
                //summary
                var qb = this.CreateFilter(); //productFilter.CreateFilter();
                var qbGroup = QueryBuilder.Create<Inspection>();
                var summary = Service.Summary(qb, qbGroup);
                productSummary.Model = summary;
                productSummary.DataBind();
                udpSummary.Update();
            },
            c =>
            {
                udpHeader.Alert(c.Exception);
            });

            this.Setup(exporter, new ExportService());

        }

    }
}
